#ifndef REGBASE_H
#define REGBASE_H

/**
 * @copyright Copyright (c) 2025 
 * CSG Digital Grid Technology (Guangdong) Co., Ltd.
 * @author zhouyan
 * @brief 数据转换类
*/
#include "dx_type.h"

class IDevice;
class RegBase
{
public:
    enum
    {
        INIT = -1,
        VALID,
        INVALID
    };
    RegBase(const std::string &name);
    virtual ~RegBase();

    const std::string &name(void) const {return name_;}
    virtual DXType rawDataType(void) const = 0;

    virtual bool isBool(void) const { return false; }
    virtual bool isInt(void) const { return false; }
    virtual bool isUint(void) const { return false; }
    virtual bool isFloat(void) const { return false; }
    virtual bool isString(void) const { return false; }
    virtual bool isNumber(void) const { return false; }

    virtual float toFloat(void) const = 0;
    virtual std::string toString(void) const = 0;
    virtual int toInt(void) const { return -1; }
    virtual uint32_t toUint(void) const { return static_cast<uint32_t>(toInt()); }

    virtual bool changed(bool force = false) = 0;
    bool actived(void) const {return (reg_status_ == VALID) ? (true) : (false);}
    int status(void) const {return reg_status_;}
    void setStatus(int newStatus) {reg_status_ = newStatus;}

    bool enable() {return enabled;}
    void setEnable(bool theEnabled) {enabled = theEnabled;}
    virtual uint8_t setRawData(const void *buf) = 0;
    virtual void setRawData(bool val) = 0;
    virtual void setRawData(int val) = 0;
    virtual void setRawData(float val) = 0;
    virtual void setRawData(const char *str) = 0;
    virtual void setRawData(const std::string &str) { setRawData(str.c_str()); }

    virtual void setRealData(bool val) = 0;
    virtual void setRealData(int val) = 0;
    virtual void setRealData(float val) = 0;
    virtual void setRealData(const char *str) = 0;
    virtual void setRealData(const std::string &str) { setRawData(str.c_str()); }

    virtual int toRawDataInt(void) const = 0;
    virtual float toRawDataFloat(void) const = 0;

    virtual RegBase *newReg(const std::string &name) = 0;
    virtual bool fillData(const uint8_t *data, uint32_t len, IDevice *dev, bool dump);
    virtual float k() const = 0;
    virtual float b() const = 0;

protected:
    bool enabled;               //是否开启采集本项数据，默认禁止，需要数据中心映射
    std::string name_;
    int reg_status_;
    int transfer_reg_status_;
};

struct RegInfos : public std::list<RegBase *>
{
    RegInfos()
    : tm(0),
      polling_cycle(0)
    {
        shell.clear();
    }

    time_t          tm;
    uint32_t        polling_cycle;
    DXType          type;
    double          rate;
    double          b;
    bool            val;

    std::string     shell;

    bool check(time_t tm);
    bool fillData(const uint8_t *data, uint32_t len, IDevice *dev, bool dump);

    iterator operator [](uint32_t);
};

RegBase *findReg(RegInfos &regs, const std::string &name);
const RegBase *findReg(const RegInfos &regs, const std::string &name);

#endif // REGBASE_H
